﻿package com.shangda.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.shangda.dao.SewageDao;
import com.shangda.entity.Sewage;
import com.shangda.util.JdbcUtil;

public class SewageDaoImpl implements SewageDao{

	//插入操作
	public void insertSewage(int sewageId) throws Exception {
		
	}

	//判断sewageId是否存在
	public boolean isExist(int sewageId) throws Exception {
		Connection con = JdbcUtil.getConnection();
		String sql = "select * from sewage where sewageID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, sewageId);
		ResultSet rs = ps.executeQuery();
		boolean flag = false;
		if (rs.next()){
			flag = true;
		}
		JdbcUtil.release(rs, ps, con);
		
		return flag;
	}

	@Override
	public Sewage findById(int sewageId) throws Exception {
		Connection con = JdbcUtil.getConnection();
		String sql = "select *from sewage where sewageID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, sewageId);
		ResultSet rs = ps.executeQuery();
		Sewage sewage = null;
		if (rs.next()){
			sewage = new Sewage();
			sewage.setSewageId(rs.getInt("sewageID"));
			sewage.setRuntimeperiod1(rs.getInt("runtimeperiod1"));
			sewage.setStoptimeperiod1(rs.getInt("Stoptimeperiod1"));
			sewage.setRuntimeperiod2(rs.getInt("runtimeperiod2"));
			sewage.setStoptimeperiod2(rs.getInt("Stoptimeperiod2"));
			sewage.setRuntimeperiod3(rs.getInt("runtimeperiod3"));
			sewage.setStoptimeperiod3(rs.getInt("Stoptimeperiod3"));
			sewage.setRuntimeperiod4(rs.getInt("runtimeperiod4"));
			sewage.setStoptimeperiod4(rs.getInt("Stoptimeperiod4"));
			sewage.setRuntimeperiod5(rs.getInt("runtimeperiod5"));
			sewage.setStoptimeperiod5(rs.getInt("Stoptimeperiod5"));
			sewage.setRuntimeperiod6(rs.getInt("runtimeperiod6"));
			sewage.setStoptimeperiod6(rs.getInt("Stoptimeperiod6"));
			sewage.setRuntimeperiod7(rs.getInt("runtimeperiod7"));
			sewage.setStoptimeperiod7(rs.getInt("Stoptimeperiod7"));
			
		}
		
		JdbcUtil.release(rs, ps, con);
		
		return sewage;
	}

	@Override
	public boolean updateRunTime(int sewageId,int... times) throws Exception {
		
		Connection con = JdbcUtil.getConnection();
		String sql = "update sewage set runtimeperiod1 =?, Stoptimeperiod1 = ?," +
				     "runtimeperiod2 =?, Stoptimeperiod2 = ?,runtimeperiod3 =?, Stoptimeperiod3 = ?," +
				     "runtimeperiod4 =?, Stoptimeperiod4 = ?,runtimeperiod5 =?, Stoptimeperiod5 = ?," +
				     "runtimeperiod6 =?, Stoptimeperiod6 = ?,runtimeperiod7 =?, Stoptimeperiod7 = ? " +
				     "where sewageId =?";
		PreparedStatement ps = con.prepareStatement(sql);
		for (int i= 1;i<=14;i++){
			ps.setInt(i, times[i-1]);
		}
		
		ps.setInt(15, sewageId);
		ps.execute();
		
		return true;
	}
	
	public static void main(String[] args) {
		SewageDao dao = new SewageDaoImpl();
		int [] times = new int [] {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
		int sewageId = 22;
		try {
			dao.updateRunTime(sewageId, times);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
